<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\XApiUser;
use Swoole\Coroutine\Channel;
use Hyperf\Utils\Exception\ParallelExecutionException;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Parallel;

use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
class IndexController extends AbstractController
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        parent::__construct();
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }
    public function index()
    {
        $parallel = new Parallel();
        $parallel->add(function () {
            $list = XApiUser::find(763637);
            return $list->user_name;
        });
        $parallel->add(function () {
            $list = XApiUser::find(763638);
            return $list->user_name;
        });

        try{
            // $results 结果为 [1, 2]
            $results = $parallel->wait();
            return $results;
        } catch(ParallelExecutionException $e){
            // $e->getResults() 获取协程中的返回值。
            // $e->getThrowables() 获取协程中出现的异常。
        }
    }

    private function setLog(){
        $list = XApiUser::all();
        $this->logger->info(1);
    }
}
